package com.mypro.util;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;

/**
 * Author: 玄夏
 * Date: 2018/4/28 15:47
 * Description:
 */
public class TreeBuilder {
    /**
     * 两层循环实现建树
     *
     * @param treeNodes 传入的树节点列表
     * @return
     */
    public static TreeNode bulid(List<TreeNode> treeNodes) {
        TreeNode root = null;
        for (TreeNode treeNode : treeNodes) {
            if (treeNode.getParentId() == 0) {
                root = treeNode;
            }
            for (TreeNode it : treeNodes) {
                if (it.getParentId() == treeNode.getId()) {
                    if (treeNode.getChildren() == null) {
                        treeNode.setChildren(new ArrayList<TreeNode>());
                    }
                    treeNode.getChildren().add(it);
                }
            }
        }
        return root;
    }

    /**
     * 使用递归方法建树
     *
     * @param treeNodes
     * @return
     */
    public static List<TreeNode> buildByRecursive(List<TreeNode> treeNodes) {
        List<TreeNode> trees = new ArrayList<TreeNode>();
        for (TreeNode treeNode : treeNodes) {
            if ("0".equals(treeNode.getParentId())) {
                trees.add(findChildren(treeNode, treeNodes));
            }
        }
        return trees;
    }

    /**
     * 递归查找子节点
     *
     * @param treeNodes
     * @return
     */
    public static TreeNode findChildren(TreeNode treeNode, List<TreeNode> treeNodes) {
        for (TreeNode it : treeNodes) {
            if (treeNode.getId().equals(it.getParentId())) {
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList<TreeNode>());
                }
                treeNode.getChildren().add(findChildren(it, treeNodes));
            }
        }
        return treeNode;
    }

    public static void main(String[] args) {

        TreeNode treeNode1 = new TreeNode(1L, "商企通业务部", 0L);

        TreeNode treeNode3 = new TreeNode(3L, "分销组", 1L);
        TreeNode treeNode4 = new TreeNode(4L, "技术组", 1L);
        TreeNode treeNode5 = new TreeNode(5L, "商务组", 1L);
        TreeNode treeNode6 = new TreeNode(6L, "餐饮组", 4L);
        TreeNode treeNode7 = new TreeNode(7L, "外卖组", 4L);


        List<TreeNode> list = new ArrayList<TreeNode>();

        list.add(treeNode1);
        list.add(treeNode3);
        list.add(treeNode4);
        list.add(treeNode5);
        list.add(treeNode6);
        list.add(treeNode7);

        long t1 = System.currentTimeMillis();
        TreeNode root = TreeBuilder.bulid(list);
        System.out.println(JSON.toJSONString(root));
        long t2 = System.currentTimeMillis();
        System.out.println(t2-t1);
        List<TreeNode> trees_ = TreeBuilder.buildByRecursive(list);
        long t3 = System.currentTimeMillis();
        System.out.println(t3-t2);

    }
}

class TreeNode {
    private Long id;
    private Long parentId;
    private String name;
    private List<TreeNode> children;

    public TreeNode(Long id, String name, Long parentId) {
        this.id = id;
        this.parentId = parentId;
        this.name = name;
    }

    public TreeNode(Long id, String name, TreeNode parent) {
        this.id = id;
        this.parentId = parent.getId();
        this.name = name;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public List<TreeNode> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }

    @Override
    public String toString() {
        return "TreeNode{" + "id='" + id + '\'' + ", parentId='" + parentId + '\'' + ", name='" + name + '\'' + ", children=" + children + '}';
    }

}

